//
//  MatchTVC.m
//  Match
//
//  Created by lanou3g on 15/5/31.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "MatchTVC.h"
#import "NewsModel.h"
#import "NewsSectionModel.h"
#import "NSMutableArray+AFNetGet.h"
#import "MatchCell.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"

#import "MatchVC.h"

@interface MatchTVC ()
@property (nonatomic, strong) NSMutableArray *allNewsArray;
@property (nonatomic,strong) NSString *data_block;

@property (nonatomic,strong) NSString *firstDay;
@property (nonatomic,strong) NSString *lastDay;
@property (nonatomic,strong) NSMutableArray *allDayArray;
@property (nonatomic,assign) NSInteger minDay;
@property (nonatomic,assign) NSInteger maxDay;
@end

@implementation MatchTVC
-(instancetype)initWithStyle:(UITableViewStyle)style useHomePW:(NSString *)homePWeb refreshFrontWeb:(NSString *)refreshFWeb behindWeb:(NSString *)behindWeb detailWeb:(NSString *)detailWeb{
    
    if (self = [super initWithStyle:style]) {
     
        self.homePageWeb = homePWeb;
        self.refreshFrontWeb = refreshFWeb;
        self.behindWeb = behindWeb;
        self.detailWeb = detailWeb;
        }
    return self;
    
}




- (NSMutableArray *)allDayArray{
    if (_allDayArray == nil) {
        _allDayArray = [NSMutableArray array];
    }
    return _allDayArray;
}

- (void)viewDidLoad {
    
    
    [super viewDidLoad];
    __weak typeof(self) weakSelf = self;
    [NSMutableArray getMutableArrayWithUrl:self.homePageWeb invokeDay:@"1" tableView:self.tableView success:^(NSMutableArray *returnArray, NSString *data_block ,NSString *lastDay) {
      
        weakSelf.firstDay= data_block;
        weakSelf.lastDay = lastDay;
        [weakSelf.allDayArray addObject:data_block];
        weakSelf.allNewsArray = [NSMutableArray arrayWithArray:returnArray];
        
        
        
        [weakSelf.tableView reloadData];
        
    }];
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"MatchCell" bundle:nil] forCellReuseIdentifier:@"matchCell"];
    
   

    [self setupRefresh];
    
    
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)setupRefresh{
    
    
    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing) dateKey:@"header"];
    [self.tableView addFooterWithTarget:self action:@selector(footerRereshing)];
    
    
}
- (void)headerRereshing{
    
    __weak typeof(self) weakSelf = self;
    
    [NSMutableArray getMutableArrayWithUrl:[NSString stringWithFormat:@"%@%ld%@",self.refreshFrontWeb,[weakSelf.firstDay integerValue]-1,self.behindWeb] invokeDay:weakSelf.firstDay tableView:self.tableView success:^(NSMutableArray *returnArray, NSString *data_block ,NSString*lastDay) {
        weakSelf.firstDay = data_block;
        [weakSelf.allDayArray addObject:data_block];
       
        
        NSInteger section = returnArray.count;
        weakSelf.lastDay = lastDay;
        [returnArray addObjectsFromArray:weakSelf.allNewsArray];
        weakSelf.allNewsArray = returnArray;
        
        
        [weakSelf.tableView reloadData];
        [weakSelf.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section+1] animated:NO scrollPosition:(UITableViewScrollPositionMiddle)];

        [self.tableView headerEndRefreshing];
               
    }];

    
}

- (void)footerRereshing{
    
    __weak typeof(self) weakSelf = self;
    
    [NSMutableArray getMutableArrayWithUrl:[NSString stringWithFormat:@"%@%ld%@",self.refreshFrontWeb,[weakSelf.lastDay integerValue],self.behindWeb] invokeDay:weakSelf.lastDay tableView:self.tableView success:^(NSMutableArray *returnArray, NSString *data_block ,NSString*lastDay) {
        

        [weakSelf.tableView footerEndRefreshing];

    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.

    return self.allNewsArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    // Return the number of rows in the section.
    NewsSectionModel *model = _allNewsArray[section];
    
    
    
    return model.array.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    
    MatchCell *cell = [tableView dequeueReusableCellWithIdentifier:@"matchCell" forIndexPath:indexPath];
 
    
  
    NewsSectionModel *newsSM = _allNewsArray[indexPath.section];
    
           NewsModel*model = newsSM.array[indexPath.row];
    model.detailWeb = self.detailWeb;
        cell.newsModel = model;
       
        
        [cell.LeftImageV sd_setImageWithURL:[NSURL URLWithString:model.home_logo] placeholderImage:[UIImage imageNamed:@"placeHold"]];
        
        [cell.rightImageV sd_setImageWithURL:[NSURL URLWithString:model.away_logo] placeholderImage:[UIImage imageNamed:@"placeHold"]];

    
   
    
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 145;
}


#pragma   ----设置区头的view----
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.tableView.frame), 35)];

    NewsSectionModel * model = _allNewsArray[section];
    UILabel *dataLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, 130, 22)];
    dataLabel.textAlignment = NSTextAlignmentLeft;
    
    UILabel *typeLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetWidth([UIScreen mainScreen].bounds)-210, 0,200, 22)];
    typeLabel.textAlignment = NSTextAlignmentRight;
    dataLabel.text = model.date_block;

    typeLabel.text = model.type_block;
    [view addSubview:dataLabel];
    [view addSubview:typeLabel];
    view.backgroundColor = [UIColor colorWithRed:60/256.0 green:90/256.0 blue:121/256.0 alpha:1.0];
    return view;
    
    
    
}


#pragma  ---被选中的时候---
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NewsSectionModel *sectionModel = _allNewsArray[indexPath.section];
    
    NewsModel *model = sectionModel.array[indexPath.row];
    
    MatchVC *matchViewC = [[MatchVC alloc]init];
    matchViewC.detailWeb = model.detailWeb;

    
    matchViewC.view.backgroundColor = [UIColor blueColor];
        matchViewC.model = model;
  
    [self.navigationController pushViewController:matchViewC animated:YES];
    
     
    
    
}








/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Table view delegate

// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here, for example:
    // Create the next view controller.
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:<#@"Nib name"#> bundle:nil];
    
    // Pass the selected object to the new view controller.
    
    // Push the view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
